package com.app.business.service.newTextForCs;

import com.app.business.model.newTextForCs.TextHouseHoldAndArea;
import com.app.business.model.newTextForCs.TextParameter;
import com.app.system.jpa.JpaBaseDao;
import com.app.system.jpa.PageBean;
import com.app.system.jpa.Wrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.app.system.utils.ParamUtils;
import com.app.system.utils.WebUtils;
import com.app.system.utils.dataType.BigDecimalUtils;
import com.app.system.utils.exception.Precondition;
import com.app.system.utils.exception.Response;

import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;
import java.util.List;

/**
 * 测算配置管理
 * Created by ddz on 2020/1/20.
 */
@SuppressWarnings(value = "ALL")
@Service
@Transactional
public class TextParameterService extends JpaBaseDao<TextParameter, Integer> {

    @Autowired
    private TextHouseHoldAndAreaService textHouseHoldAndAreaService;

    /**
     * 分页处理
     *
     * @return
     */
    public PageBean<TextParameter> getTextParameterPageBean() {
        //查询语句
        StringBuffer query = new StringBuffer("p from TextParameter p ");

        //默认排序(PageBean指定排序字段类型的话，则默认排序不使用)
        StringBuffer orderBy = new StringBuffer("p.parameterCode");
        Wrapper wrapper = new Wrapper(query, orderBy);
        wrapper.initSearchParams();

        return this.pageHql(wrapper);
    }


    /**
     * 根据协议编号查询协议
     *
     * @return
     */
    public TextParameter getTextParameterByParameterCode(String parameterCode) {
        List<TextParameter> list = this.findEqualField("parameterCode", parameterCode);
        if (list.size() > 0 && list != null) {
            return list.get(0);
        } else {
            return null;
        }
    }

    /**
     * 征收项目参数配置数据保存
     *
     * @param request
     * @return
     */
    public synchronized Response dataFormSave(HttpServletRequest request) {
        int textParameterId = ParamUtils.getInt(request, "textParameterId", 0);
        TextParameter textParameter = this.findOne(textParameterId);
        if (textParameter == null) {
            textParameter = new TextParameter();
        }
        WebUtils.bind(request, textParameter);

        Precondition.checkAjaxArguement(textParameter != null, "1111", "未找到相关配置，保存失败！");

        this.save(textParameter);
        //保存之前先删除数据
        this.textHouseHoldAndAreaService.deleteTextHouseHoldAndAreaByTextParameterId(textParameter.getId());

        String[] sortNumArray = ParamUtils.getStringParameters(request, "sortNum");    //排序号
        String[] houseHoldArray = ParamUtils.getStringParameters(request, "houseHold");    //户型
        String[] areaArray = ParamUtils.getStringParameters(request, "area");    //面积
        String[] minAreaArray = ParamUtils.getStringParameters(request, "minArea");    //最小允许面积
        //必须不为空，且长度一致（必填）
        Precondition.checkAjaxArguement(areaArray != null && houseHoldArray != null && sortNumArray != null && minAreaArray != null, "2222", "面积户型数据填写不规范！");
        int length = sortNumArray.length;
        String sortNum = null, houseHold = null, area = null, minArea = null;
        for (int i = 0; i < length; i++) {
            sortNum = null;
            houseHold = null;
            area = null;
            minArea = null;
            sortNum = sortNumArray[i];
            houseHold = houseHoldArray[i];
            area = areaArray[i];
            minArea = minAreaArray[i];
            if (sortNum != null && !sortNum.equals("") && houseHold != null && !houseHold.equals("") && area != null && !area.equals("") && minArea != null && !minArea.equals("")) {
                TextHouseHoldAndArea textHouseHoldAndArea = new TextHouseHoldAndArea();
                textHouseHoldAndArea.setSortNum(Integer.valueOf(sortNum));
                textHouseHoldAndArea.setArea(BigDecimalUtils.stringToBigDecimal(area));
                textHouseHoldAndArea.setMinArea(BigDecimalUtils.stringToBigDecimal(minArea));
                textHouseHoldAndArea.setHouseHold(houseHold);
                textHouseHoldAndArea.setTextParameter(textParameter);   //征收项目
                this.textHouseHoldAndAreaService.save(textHouseHoldAndArea);
            }
        }
        return new Response("0001", "测算成功!");
    }


}
